package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.CommentPostBo;
import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.db.domain.MarketUser;
import com.cskaoyan.market.service.WxCommentService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.CommentInfoVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: chenjian
 * @Date: 2024/5/20 8:54
 */
@RestController
@RequestMapping("/wx/comment/")
public class WxCommentController {
    @Autowired
    WxCommentService wxCommentService;
    @Autowired
    SecurityManager securityManager;
    //Request URL: http://39.101.189.16:8083/wx/comment/list?valueId=1022000&type=0&limit=20&page=1&showType=0
    @GetMapping("list")
    public Object list(int valueId, int type, int limit, int page, int showType){
        try {
            List<CommentInfoVo> commentList = wxCommentService.list(valueId, type, limit, page, showType);
            return ResponseUtil.okList(commentList);
        }catch (Exception e) {
            return ResponseUtil.badArgument();
        }
    }

    //?valueId=1022000&type=0
    @GetMapping("count")
    public Object count(int valueId, int type){
        try {
            Map<String, Integer> count = wxCommentService.count(valueId, type);
            return ResponseUtil.ok(count);
        }catch (Exception e) {
            return ResponseUtil.badArgument();
        }
    }

    @PostMapping("post")
    public Object post(@RequestBody CommentPostBo commentPostBo){
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("user");
        /*MarketUser user = new MarketUser();
        user.setId(1);*/

        try {
            MarketComment marketComment= wxCommentService.post(commentPostBo,user.getId());
            return ResponseUtil.ok(marketComment);
        }catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.badArgument();
        }
    }
}
